package ovm.gui.actions.authorization;

import com.vaadin.terminal.ExternalResource;
import ovm.application.MainApplication;
import ovm.gui.AuthorizationWindow;
import com.vaadin.ui.Button;
import ovm.management.security.AuthorizationChecker;

public class AuthorizeButtonAction implements Button.ClickListener {

    private AuthorizationWindow window;

    public AuthorizeButtonAction (AuthorizationWindow window) {
        this.window = window;
    }

    @Override
    public void buttonClick(Button.ClickEvent event) {
        String loginTextValue = (String) window.getLoginText().getValue();
        String passwordTextValue = (String) window.getPasswordText().getValue();
        int userRule = AuthorizationChecker.checkAuthorization(loginTextValue, passwordTextValue);
        if (userRule == -1) {
            window.showNotification("Неверный логин или пароль");
        } else {
            window.getApplication().createNavigator(userRule);
            window.open(new ExternalResource(MainApplication.navigator.getURL()));
        }
    }
}
